package com.wjh.graduation.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wjh.graduation.domain.R;
import com.wjh.graduation.domain.Remind;
import com.wjh.graduation.service.RemindService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Wu Jiahong
 * @since 2022-01-25
 */
@RestController
@RequestMapping("/remind")
@Api(tags = "提醒事项")
public class RemindController {

	@Autowired
	RemindService remindService;

	@GetMapping("/getRemind")
	@ApiOperation("获得用户事务")
	public R<List<Remind>> getGroup(@ApiParam("传入用户的id") @RequestParam Integer id) {
		QueryWrapper<Remind> queryWrapper = new QueryWrapper<>();
		//排序
		queryWrapper.select("remind_id", "name", "time").eq("user_id", id).orderByDesc("time");
		List<Remind> list = remindService.list(queryWrapper);
		return new R<List<Remind>>(200, list, "获取成功");
		//获取失败list也为空
		/*if (list.isEmpty()) {
			return new R(303, null, "获取失败或");
		} else {
			return new R<List<Remind>>(200, list, "获取成功");
		}*/
	}

	@GetMapping("/getRemindD")
	@ApiOperation("获得事务详情")
	public R<Remind> getGroupD(@ApiParam("传入事务的id") @RequestParam Integer id) {
		Remind remind = remindService.getById(id);
		if (remind != null) {
			return new R<Remind>(200, remind, "获取成功");
		} else {
			return new R<Remind>(303, null, "获取失败");
		}
	}

	@ApiOperation("创建提醒")
	@PostMapping("/create")
	public R create(@RequestBody @Validated @ApiParam(name = "传入事务实体的字段，userId,time,name为必填项") Remind remind) {

		if (remindService.save(remind)) {
			return new R(200, "添加成功");
		} else {
			return new R(303, "添加失败");
		}
	}


	@ApiOperation("删除提醒")
	@GetMapping("/delete")
	public R deleteRemind(@ApiParam("传入分组的的ID") @RequestParam Integer id) {
		if (remindService.removeById(id)) {
			return new R(200, "删除成功");
		} else {
			return new R(303, "删除失败");
		}
	}

	@ApiOperation("修改提醒")
	@PostMapping("/update")
	public R updateGroup(@Validated @ApiParam(name = "传入事务实体的字段，userId,time,name为必填项") @RequestBody Remind remind) {
		System.out.println("info" + remind);
		if (remindService.updateById(remind)) {
			return new R(200, "修改成功");
		} else {
			return new R(303, "修改失败");
		}
	}
}

